package com.android.clinic.app.utils;

import android.app.Activity;
import android.content.Context;

import com.android.clinic.app.ClinicApplication;
import com.android.clinic.app.entity.DepartmentInfo;
import com.jiongbull.jlog.JLog;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cheng on 2018/1/26.
 */
public class LocalDataUtils {

    public static Map<Integer, List<DepartmentInfo>> objectMap = new HashMap<>();

    /**
     * 本地保存数据
     *
     * @param context
     * @param file
     * @param object
     * @return
     */
    public static void saveDataToLocate(Context context, String file, Object object) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Activity.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(object);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                    fos.close();
                }
                JLog.e("保存成功");
            } catch (Exception e) {
                JLog.e("没有数据");
                e.printStackTrace();
            }
        }
    }

    /**
     * 本地读取数据
     *
     * @param context
     * @param file
     * @return
     */
    public static Object loadDataFromLocate(Context context, String file) {
        Object obj = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(file);
            ois = new ObjectInputStream(fis);
            obj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            obj = null;
        } finally {
            try {
                if (fis != null) {
                    ois.close();
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return obj;
    }

    /**
     * 保存数据
     * @param key
     * @param value
     */
    public static void saveData(String key, String value) {
        ClinicApplication.getInstance().mACache.put(key, value);// 缓存
    }

    /**
     * 保存数据
     * @param key
     * @param value
     * @param saveTime
     */
    public static void saveData(String key, String value, int saveTime) {
        ClinicApplication.getInstance().mACache.put(key, value, saveTime);// 缓存
    }

    /**
     * 获取数据
     * @param key
     * @return
     */
    public static String getData(String key) {
        String value = "";
        if (ClinicApplication.getInstance().mACache.getAsString(key) != null) {
            value = ClinicApplication.getInstance().mACache.getAsString(key);
        }
        return value;
    }

    /**
     * 删除数据
     * @param key
     */
    public static void removeData(String key) {
        ClinicApplication.getInstance().mACache.remove(key);
    }

}
